//#include "global_def.h"  // These don't seem to work.  They should.
//#include "ms_remote_map.h" // These don't seem to work.  They should.
#include <IRremote.h>
//#define ms_remote_map.h
//below mappings are for Microsoft Media Center Remote
#define OK_POS1 0x800F0422
#define OK_POS2 0x800F8422
#define PWR_POS1 0x800F040C
#define PWR_POS2 0x800F840C
#define VOL_DWN_POS1 0x800F041F
#define VOL_DWN_POS2 0x800F841F
#define VOL_UP_POS1 0x800F041E
#define VOL_UP_POS2 0x800F841E
#define MAX_BRIGHT 255
#define SL_BD_RT 9600

#define RECV_PIN 4  //changed from 11 to 4
#define LED_PIN 5 // choose the pin for the LED

int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead=255;//define the spead of motor

byte ledState;
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean power_state = LOW;

void setup(){
  Serial.begin(SL_BD_RT);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_PIN, OUTPUT);  // declare LED as output
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}

void forward()
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);
}
void backward()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);
}
void left()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
     digitalWrite(pinI1,LOW);
}
void right()//
{
     analogWrite(speedpinA,spead);//input a simulation value to set the speed
     analogWrite(speedpinB,spead);
     digitalWrite(pinI4,LOW);//turn DC Motor B move anticlockwise
     digitalWrite(pinI3,HIGH);
     digitalWrite(pinI2,LOW);//turn DC Motor A move clockwise
     digitalWrite(pinI1,HIGH);
}
void stop()//
{
     digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
     digitalWrite(speedpinB,LOW);
     delay(1000);
 
}


void loop() {
  if (irrecv.decode(&results)) {  //If IR receive results are detected
    Serial.println(results.value, HEX);
    switch (results.value) {
     //Power
      
    case OK_POS1:
         analogWrite(speedpinA,spead);//input a simulation value to set the speed
    analogWrite(speedpinB,spead);
     digitalWrite(pinI4,HIGH);//turn DC Motor B move clockwise
     digitalWrite(pinI3,LOW);
     digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
     digitalWrite(pinI1,HIGH);
  //delay(2000);
 
    break;
    case OK_POS2:
   
            digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
     digitalWrite(speedpinB,LOW);
//     delay(1000);
    break;
    case PWR_POS1:
           digitalWrite(speedpinA,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
     digitalWrite(speedpinB,LOW);
//     delay(1000);
    break;
    //Dim
    case VOL_DWN_POS1:
              if (spead > 10){
            spead -= 10;
          }
         
//          if (spead > 0){
//          analogWrite(LED_PIN, ledState);  // Dim LED
//          }
    break;
    case VOL_DWN_POS2:
              if (spead > 10){
            spead -= 10;
          }
        
        
//         if (spead > 0){
//         analogWrite(LED_PIN, ledState);  // Dim LED
//         }
    break;
    //Brighten
    case VOL_UP_POS1:
              if (spead < MAX_BRIGHT){
            spead += 10;
          }
      /*    if (ledState < MAX_BRIGHT){
            ledState += 10;
          }
         analogWrite(LED_PIN, ledState);  // Brighten LED */
    break;
   case VOL_UP_POS2:
          if (spead < MAX_BRIGHT){
            spead += 10;
          }
//         analogWrite(LED_PIN, ledState);  // Brighten LED
    break;
    //default:
     //  Serial.println("");
  }
    delay(200);  // 1/5 second delay for arbatrary clicks.  Will implement debounce later.
    irrecv.resume(); // Receive the next value
  }
}
